package com.fuce.one.deploy.util;

import com.fuce.one.deploy.util.bat.StreamGobbler;

public class BatUtil {
	/**
	 * 执行批处理方法
	 * @param batStr
	 * @author fuce
	 * @Date 2020年3月2日 上午2:33:19
	 */
	public static String bat(String batStr) {
		Runtime rt = Runtime.getRuntime();
		Process ps = null;
		StreamGobbler outGobbler=null;
		try {
		    ps = rt.exec("cmd.exe /C star /b " + batStr);//batDir是bat文件路径
		    //必要的，不然会阻塞
		    StreamGobbler errorGobbler = new StreamGobbler(ps.getErrorStream(), "ERROR");
		    errorGobbler.start();
		    //好像没什么用
		    outGobbler = new StreamGobbler(ps.getInputStream(), "STDOUT");
		    outGobbler.start();
		   
		    ps.waitFor();
		    
		    
		   
		} catch (Exception e1) {
		    e1.printStackTrace();
		}

		int success = ps.exitValue();
		if (success == 0) {
		    //启动成功
			System.out.println(">>>>>>>>>");
			return outGobbler.getRunStr();
		} else {
		    //启动失败
			System.out.println("<<<<<<<<<<<");
			return "error";
		}
		
	}
	
	public static void main(String[] args) {
		//String string="Date /T";
		String string="D:/java/tomcat服务器版本/Alibaba_Dragonwell/apache-tomcat-8.5.47/bin/startup.bat";
		System.out.println(bat(string));
	}
}
